package com.pshcp.api;

import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.common.config.CustomConf;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.pshcp.common.tool.FileTool;
import com.pshcp.vo.file.FileListVO;
import com.pshcp.vo.file.FileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 通用接口-附件
 * @Author: xiaoche
 * @Date: 2022/11/3 11:58
 */
@Api(tags = {"通用接口-附件"})
@Slf4j
@RestController
@RequestMapping("/api/file")
public class FileAPI {

    @Autowired
    private CustomConf customConf;

    @ApiOperation("文件上传")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "文件上传")
    @PostMapping("upload")
    @ApiImplicitParam(name = "type", value = "业务类型0医生文章1医生头像2患者头像3banner4就诊")
    public ResultDTO<FileVO> upload(@ApiParam(value = "附件") @RequestPart("file") MultipartFile file,
            Integer type) throws BizException {
        CommonTool.checkObj(type, "10020001");
        CommonTool.checkObj(file, "10020002");
        String prefix = FileTool.getPrefix(type);
        CommonTool.checkString(prefix, "10020003");
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String path = "/" + prefix + "/" + SystemUtil.getUUID() + "." + suffix;
        String filePath = customConf.getUploadFilePath() + path;
        File dest = new File(filePath);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        FileVO fileVO = new FileVO();
        fileVO.setFileUrl(customConf.getFileUrl());
        fileVO.setFilePath(path);
        return ResultBuilder.success(fileVO);
    }

    @ApiOperation("多文件上传")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "多文件上传")
    @PostMapping("uploads")
    @ApiImplicitParam(name = "type", value = "业务类型0医生文章1医生头像2患者头像3banner4就诊")
    public ResultDTO<FileListVO> uploads(@ApiParam(value = "附件") @RequestPart("file") MultipartFile[] files,
                                        Integer type) throws BizException {
        CommonTool.checkObj(type, "10020001");
        CommonTool.checkObj(files, "10020002");
        String prefix = FileTool.getPrefix(type);
        CommonTool.checkString(prefix, "10020003");
        List<String>paths=new ArrayList<>();
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            if (StringUtils.isBlank(file.getOriginalFilename())){
                continue;
            }
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            String path = "/" + prefix + "/" + SystemUtil.getUUID() + "." + suffix;
            String filePath = customConf.getUploadFilePath() + path;
            File dest = new File(filePath);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            paths.add(path);
        }
        FileListVO fileListVO = new FileListVO();
        fileListVO.setFileUrl(customConf.getFileUrl());
        fileListVO.setFilePathList(paths);
        return ResultBuilder.success(fileListVO);
    }
}
